package huawei.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Insets;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable.Callback;
import android.graphics.drawable.DrawableContainer;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.util.FloatProperty;
import android.util.Property;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.Switch;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import huawei.android.widget.DecouplingUtil.ReflectUtil;
import huawei.hwanimation.CubicBezierInterpolator;
import java.util.Locale;

public class HwSwitch extends Switch {
    private static final Property<HwSwitch, Float> ˎ = new FloatProperty<HwSwitch>("thumbPos") {
        public /* synthetic */ Object get(Object obj) {
            return ˎ((HwSwitch) obj);
        }

        public /* synthetic */ void setValue(Object obj, float f) {
            ˋ((HwSwitch) obj, f);
        }

        public Float ˎ(HwSwitch hwSwitch) {
            return Float.valueOf(hwSwitch.ͺ);
        }

        public void ˋ(HwSwitch hwSwitch, float f) {
            hwSwitch.ˋ(f);
        }
    };
    private int ʻ;
    private VelocityTracker ʻॱ;
    private int ʼ;
    private int ʽ;
    private Drawable ˊ;
    private int ˊॱ;
    private ObjectAnimator ˋ;
    private int ˋॱ;
    private Drawable ˏ;
    private int ˏॱ;
    private float ͺ;
    private int ॱ;
    private int ॱˊ;
    private final Rect ॱˋ;
    private float ॱˎ;
    private int ॱॱ;
    private int ॱᐝ;
    private int ᐝ;
    private float ᐝॱ;

    public HwSwitch(Context context) {
        this(context, null);
    }

    public HwSwitch(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.ॱ = 0;
        this.ॱॱ = 0;
        this.ʽ = 0;
        this.ʻ = 0;
        this.ᐝ = 0;
        this.ʼ = 0;
        this.ˏॱ = 0;
        this.ˋॱ = 0;
        this.ˊॱ = 0;
        this.ॱˊ = 0;
        this.ʻॱ = VelocityTracker.obtain();
        this.ॱˋ = new Rect();
        this.ˏॱ = (int) ((getResources().getDisplayMetrics().density * 2.0f) + 0.5f);
    }

    public HwSwitch(Context context, AttributeSet attributeSet, int i) {
        this(context, attributeSet, i, 0);
    }

    public HwSwitch(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i);
        this.ॱ = 0;
        this.ॱॱ = 0;
        this.ʽ = 0;
        this.ʻ = 0;
        this.ᐝ = 0;
        this.ʼ = 0;
        this.ˏॱ = 0;
        this.ˋॱ = 0;
        this.ˊॱ = 0;
        this.ॱˊ = 0;
        this.ʻॱ = VelocityTracker.obtain();
        this.ॱˋ = new Rect();
        this.ˏॱ = (int) ((getResources().getDisplayMetrics().density * 2.0f) + 0.5f);
    }

    private void ˏ() {
        Insets insets;
        int i;
        Rect rect = this.ॱˋ;
        int i2 = this.ʽ;
        int i3 = this.ʻ;
        int i4 = this.ᐝ;
        int i5 = this.ʼ;
        if (this.ˏ == null) {
            this.ˏ = (Drawable) ReflectUtil.getObject(this, "mThumbDrawable", Switch.class);
        }
        if (this.ˊ == null) {
            this.ˊ = (Drawable) ReflectUtil.getObject(this, "mTrackDrawable", Switch.class);
        }
        Object object = ReflectUtil.getObject(this, "mThumbWidth", Switch.class);
        if (object != null) {
            this.ॱॱ = ((Integer) object).intValue();
        }
        int ˎ = this.ˏॱ + (ˎ() + i2);
        if (this.ˏ != null) {
            insets = (Insets) ReflectUtil.callMethod(this.ˏ, "getOpticalInsets", null, null, DrawableContainer.class);
            if (insets == null) {
                insets = Insets.NONE;
            }
        } else {
            insets = Insets.NONE;
        }
        if (this.ˊ != null) {
            this.ˊ.getPadding(rect);
            int i6 = rect.left + ˎ;
            if (insets != Insets.NONE) {
                if (insets.left > rect.left) {
                    i2 += insets.left - rect.left;
                }
                if (insets.top > rect.top) {
                    ˎ = (insets.top - rect.top) + i3;
                } else {
                    ˎ = i3;
                }
                if (insets.right > rect.right) {
                    i4 -= insets.right - rect.right;
                }
                i = insets.bottom > rect.bottom ? i5 - (insets.bottom - rect.bottom) : i5;
            } else {
                i = i5;
                ˎ = i3;
            }
            this.ˊ.setBounds(i2, ˎ, i4, i);
            i = i6;
        } else {
            i = ˎ;
        }
        if (this.ˏ != null) {
            this.ˏ.getPadding(rect);
            i2 = i - rect.left;
            i = (i + this.ॱॱ) + rect.right;
            Callback callback = this.ˏ.getCallback();
            this.ˏ.setCallback(null);
            this.ˏ.setBounds(i2, i3, i, i5);
            this.ˏ.setCallback(callback);
            Drawable background = getBackground();
            if (background == null) {
                return;
            }
            if (VERSION.SDK_INT >= 21) {
                background.setHotspotBounds(i2, i3, i, i5);
            } else {
                background.setBounds(i2, i3, i, i5);
            }
        }
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        int i5;
        int i6;
        int i7;
        int i8 = 0;
        super.onLayout(z, i, i2, i3, i4);
        if (this.ˏ == null) {
            this.ˏ = (Drawable) ReflectUtil.getObject(this, "mThumbDrawable", Switch.class);
        }
        if (this.ˊ == null) {
            this.ˊ = (Drawable) ReflectUtil.getObject(this, "mTrackDrawable", Switch.class);
        }
        if (this.ˏ != null) {
            Rect rect = this.ॱˋ;
            if (this.ˊ != null) {
                this.ˊ.getPadding(rect);
            } else {
                rect.setEmpty();
            }
            Insets insets = (Insets) ReflectUtil.callMethod(this.ˏ, "getOpticalInsets", null, null, DrawableContainer.class);
            i5 = insets.left - rect.left;
            if (i5 <= 0) {
                i5 = 0;
            }
            i6 = insets.right - rect.right;
            if (i6 <= 0) {
                i6 = 0;
            }
            i7 = i5;
            i5 = i6;
        } else {
            i5 = 0;
            i7 = 0;
        }
        Object object = ReflectUtil.getObject(this, "mSwitchWidth", Switch.class);
        if (object != null) {
            this.ॱ = ((Integer) object).intValue();
        }
        if (ʼ()) {
            i6 = getPaddingLeft() + i7;
            i7 = ((this.ॱ + i6) - i7) - i5;
            i5 = i6;
        } else {
            int width = (getWidth() - getPaddingRight()) - i5;
            i5 = (width - this.ॱ) + (i5 + i7);
            i7 = width;
        }
        object = ReflectUtil.getObject(this, "mSwitchHeight", Switch.class);
        if (object != null) {
            this.ॱᐝ = ((Integer) object).intValue();
        }
        switch (getGravity() & 112) {
            case 16:
                i6 = (((getPaddingTop() + getHeight()) - getPaddingBottom()) / 2) - (this.ॱᐝ / 2);
                i8 = this.ॱᐝ + i6;
                break;
            case 48:
                i6 = getPaddingTop();
                i8 = this.ॱᐝ + i6;
                break;
            case 80:
                i8 = getHeight() - getPaddingBottom();
                i6 = i8 - this.ॱᐝ;
                break;
            default:
                i6 = 0;
                break;
        }
        this.ʽ = i5;
        this.ʻ = i6;
        this.ʼ = i8;
        this.ᐝ = i7;
    }

    protected void onDraw(Canvas canvas) {
        ˏ();
        super.onDraw(canvas);
        ॱ();
    }

    private void ॱ() {
        Callback callback = this.ˏ.getCallback();
        this.ˏ.setCallback(null);
        this.ˏ.setBounds(0, 0, 0, 0);
        this.ˏ.setCallback(callback);
    }

    public void setChecked(boolean z) {
        float f = 1.0f;
        super.setChecked(z);
        boolean isChecked = isChecked();
        if (VERSION.SDK_INT >= 19) {
            if (isAttachedToWindow() && isLaidOut()) {
                ˋ(isChecked);
                return;
            }
            ˊ();
            if (!isChecked) {
                f = 0.0f;
            }
            ˋ(f);
        } else if (isClickable()) {
            ˋ(isChecked);
        } else {
            ˊ();
            if (!isChecked) {
                f = 0.0f;
            }
            ˋ(f);
        }
    }

    private void ˋ(boolean z) {
        float f = z ? 1.0f : 0.0f;
        this.ˋ = ObjectAnimator.ofFloat(this, ˎ, new float[]{f});
        this.ˋ.setDuration(200);
        this.ˋ.setInterpolator(new CubicBezierInterpolator(BarrierParameters.RESTITUTION, 1.0f, 0.9f, 1.0f));
        if (VERSION.SDK_INT >= 18) {
            this.ˋ.setAutoCancel(true);
        } else {
            this.ˋ.setRepeatCount(1);
        }
        this.ˋ.start();
    }

    private void ˋ(float f) {
        this.ͺ = f;
        invalidate();
    }

    private void ˊ() {
        if (this.ˋ != null) {
            this.ˋ.cancel();
        }
    }

    private int ˎ() {
        float f;
        if (ʽ()) {
            f = 1.0f - this.ͺ;
        } else {
            f = this.ͺ;
        }
        return (int) ((f * ((float) ˋ())) + 0.5f);
    }

    private int ˋ() {
        if (this.ˊ == null) {
            return 0;
        }
        Insets insets;
        Rect rect = this.ॱˋ;
        this.ˊ.getPadding(rect);
        if (this.ˏ != null) {
            Insets insets2 = (Insets) ReflectUtil.callMethod(this.ˏ, "getOpticalInsets", null, null, DrawableContainer.class);
            if (insets2 == null) {
                insets2 = Insets.NONE;
            }
            insets = insets2;
        } else {
            insets = Insets.NONE;
        }
        Object object = ReflectUtil.getObject(this, "mSwitchWidth", Switch.class);
        if (object != null) {
            this.ॱ = ((Integer) object).intValue();
        }
        return (((((this.ॱ - this.ॱॱ) - rect.left) - rect.right) - insets.left) - insets.right) - (this.ˏॱ + this.ˏॱ);
    }

    private boolean ˏ(float f, float f2) {
        if (this.ˏ == null) {
            return false;
        }
        int ˎ = ˎ();
        this.ˏ.getPadding(this.ॱˋ);
        Object object = ReflectUtil.getObject(this, "mTouchSlop", Switch.class);
        if (object != null) {
            this.ˊॱ = ((Integer) object).intValue();
        }
        ˎ = (ˎ + this.ʽ) - this.ˊॱ;
        boolean z = f > ((float) ˎ) && f < ((float) ((((this.ॱॱ + ˎ) + this.ॱˋ.left) + this.ॱˋ.right) + this.ˊॱ)) && f2 > ((float) (this.ʻ - this.ˊॱ)) && f2 < ((float) (this.ʼ + this.ˊॱ));
        return z;
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        this.ʻॱ.addMovement(motionEvent);
        float x;
        float y;
        switch (motionEvent.getActionMasked()) {
            case 0:
                x = motionEvent.getX();
                y = motionEvent.getY();
                if (isEnabled() && ˏ(x, y)) {
                    this.ˋॱ = 1;
                    this.ᐝॱ = x;
                    this.ॱˎ = y;
                    break;
                }
            case 1:
            case 3:
                if (this.ˋॱ != 2) {
                    this.ˋॱ = 0;
                    this.ʻॱ.clear();
                    break;
                }
                ˋ(motionEvent);
                super.onTouchEvent(motionEvent);
                return true;
            case 2:
                float y2;
                switch (this.ˋॱ) {
                    case 0:
                        break;
                    case 1:
                        y = motionEvent.getX();
                        y2 = motionEvent.getY();
                        Object object = ReflectUtil.getObject(this, "mTouchSlop", Switch.class);
                        if (object != null) {
                            this.ˊॱ = ((Integer) object).intValue();
                        }
                        if (Math.abs(y - this.ᐝॱ) > ((float) this.ˊॱ) || Math.abs(y2 - this.ॱˎ) > ((float) this.ˊॱ)) {
                            this.ˋॱ = 2;
                            getParent().requestDisallowInterceptTouchEvent(true);
                            this.ᐝॱ = y;
                            this.ॱˎ = y2;
                            return true;
                        }
                    case 2:
                        y2 = motionEvent.getX();
                        int ˋ = ˋ();
                        float f = y2 - this.ᐝॱ;
                        x = ˋ != 0 ? f / ((float) ˋ) : f > 0.0f ? 1.0f : SizeModifier.STABLE_STATE_SCALE;
                        if (ʼ()) {
                            x = -x;
                        }
                        x = ˋ(x + this.ͺ, 0.0f, 1.0f);
                        if (x != this.ͺ) {
                            this.ᐝॱ = y2;
                            ˋ(x);
                        }
                        return true;
                    default:
                        break;
                }
                break;
        }
        return super.onTouchEvent(motionEvent);
    }

    private void ˋ(MotionEvent motionEvent) {
        boolean z = true;
        this.ˋॱ = 0;
        boolean z2 = motionEvent.getAction() == 1 && isEnabled();
        boolean isChecked = isChecked();
        if (z2) {
            this.ʻॱ.computeCurrentVelocity(1000);
            float xVelocity = this.ʻॱ.getXVelocity();
            Object object = ReflectUtil.getObject(this, "mMinFlingVelocity", Switch.class);
            if (object != null) {
                this.ॱˊ = ((Integer) object).intValue();
            }
            if (Math.abs(xVelocity) <= ((float) this.ॱˊ)) {
                z = ᐝ();
            } else if (ʽ()) {
                if (xVelocity >= 0.0f) {
                    z = false;
                }
            } else if (xVelocity <= 0.0f) {
                z = false;
            }
        } else {
            z = isChecked;
        }
        if (z != isChecked) {
            playSoundEffect(0);
        }
        setChecked(z);
        ॱ(motionEvent);
    }

    private void ॱ(MotionEvent motionEvent) {
        MotionEvent obtain = MotionEvent.obtain(motionEvent);
        obtain.setAction(3);
        super.onTouchEvent(obtain);
        obtain.recycle();
    }

    private boolean ᐝ() {
        return this.ͺ > 0.5f;
    }

    private float ˋ(float f, float f2, float f3) {
        if (f < f2) {
            return f2;
        }
        return f > f3 ? f3 : f;
    }

    private boolean ʽ() {
        String language = Locale.getDefault().getLanguage();
        return language.contains("ar") || language.contains("fa") || language.contains("iw") || language.contains("ug") || language.contains("ur") || ʼ();
    }

    private boolean ʼ() {
        if (VERSION.SDK_INT < 17) {
            return false;
        }
        if (getLayoutDirection() == 1) {
            return true;
        }
        return false;
    }
}
